package com.hly.tool.attrGroup;

import com.dassault_systemes.pprRestServices.utils.ExceptionUtil;
import com.matrixone.apps.classification.ClassificationAttributesCreationUtil;
import com.matrixone.apps.domain.DomainObject;
import com.matrixone.apps.domain.util.ContextUtil;
import com.matrixone.apps.domain.util.FrameworkException;
import com.matrixone.apps.domain.util.MapList;
import com.matrixone.apps.domain.util.MqlUtil;
import com.matrixone.apps.framework.ui.UIUtil;
import matrix.db.Context;
import matrix.db.JPO;
import matrix.util.MatrixException;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author ：Jack Hao
 * @date ：Created in 2021/8/31 21:18
 * @description：库属性组工具类
 */
public class AttributeGroups
{

    /**
     * 添加新的属性组
     *
     * @param context
     * @param interfaceName
     * @param interfaceDesription
     * @return
     * @throws FrameworkException
     */
    public static Map addClassificationInterface(Context context, String interfaceName, String interfaceDesription) throws FrameworkException
    {
//        String addMql = "add interface T-AttrGroup-0001 derived 'Classification Attribute Groups' description 测试属性组-0001;
//        MqlUtil.mqlCommand(context, addMql);
        Map result = new HashMap();
        result.put("Action", "FAIL");
        result.put("Message", "");
        try
        {
            ContextUtil.pushContext(context);

            if (UIUtil.isNullOrEmpty(interfaceName))
            {
                result.put("Message", "interfaceName is null or empty");
                ContextUtil.popContext(context);
                return result;
            }

            //查询interface 是否存在
            String queryStr = "list  interface $1";
            String inResult = MqlUtil.mqlCommand(context, queryStr, interfaceName);
            if (UIUtil.isNotNullAndNotEmpty(inResult))
            {
                result.put("Message", "interfaceName is exists befor");
                ContextUtil.popContext(context);
                return result;
            }

            if (UIUtil.isNullOrEmpty(interfaceDesription))
            {
                interfaceDesription = "";
            }

            String addInterfaceMql = "add interface $1 derived $2 description $3";

            MqlUtil.mqlCommand(context, addInterfaceMql, interfaceName,"Classification Attribute Groups", interfaceDesription);

            result.put("Action", "SUCCESS");
            result.put("Message", "");

        } catch (FrameworkException e)
        {
            String stackTrace = ExceptionUtil.getStackTrace(e);
            if (stackTrace.length() > 200)
            {
                stackTrace = stackTrace.substring(0, 200);
            }
            result.put("Action", "FAIL");
            result.put("Message", stackTrace);
        } finally
        {
            ContextUtil.popContext(context);
        }


        return result;
    }


    /**
     * 查询属性组下的属性
     *
     * @param context   上下文
     * @param groupName 属性组的名称
     * @return
     */
    public static MapList getGroupAttribute(Context context, String groupName) throws MatrixException
    {
        Map programMap = new HashMap();
        programMap.put("objectName", groupName);
        String[] attrArgs = JPO.packArgs(programMap);
        //查询属性组下的属性
        MapList groutAttrList = JPO.invoke(context, "emxMultipleClassificationAttributeGroup", null, "getRelatedAttributes", attrArgs, MapList.class);
        return groutAttrList;
    }


    /**
     * 属性组添加属性
     *
     * @param context
     * @param paramMap
     */
    public static Map addGroupAttribute(Context context, Map paramMap) throws MatrixException
    {


        Map result = new HashMap();
        result.put("Action", "FAIL");
        result.put("Message", "");

        try
        {


            ContextUtil.pushContext(context);

            //属性名称
            String Name = (String) paramMap.get("Name");

            //属性组名称
            String interfaceName = (String) paramMap.get("objectName");

            //查询属性mql
            String queryAttr = "list attribute $1";
            //判断属性是否存在
            String queryAttrResult = MqlUtil.mqlCommand(context, queryAttr, Name);

            paramMap.put("timeZone", "-8");
            paramMap.put("localeObject", new Locale("zh-CN,zh"));

            //如果不存在就创建并添加到属性组
            if (UIUtil.isNullOrEmpty(queryAttrResult))
            {
                ClassificationAttributesCreationUtil.createClassificationAttribute(context, paramMap);
            } else
            {//如果已存再的就判断是不是已经添加到属性组

            }

            //查询属性组属性mql
            String queryInterfaceAttrMql = "print interface $1 select $2 dump $3";

            //查询属性组属性 结果
            String queryInterfaceAttrResult = MqlUtil.mqlCommand(context, queryInterfaceAttrMql, interfaceName, "attribute[" + Name + "]", "|");
            if (UIUtil.isNotNullAndNotEmpty(queryInterfaceAttrResult) && queryInterfaceAttrResult.toUpperCase().equals("TRUE"))
            {//属性组已添加属性

            } else
            {//属性组未添加已存再的属性
                //            String addInterfaceAttrMql = "mod interface '" + objectName + "' add attribute '" + Name + "'";
                String addInterfaceAttrMql = "mod interface $1 add attribute $2";
                //属性组添加属性
                MqlUtil.mqlCommand(context, addInterfaceAttrMql, interfaceName, Name);
            }

            result.put("Action", "SUCCESS");
            result.put("Message", "");
        } catch (FrameworkException e)
        {
            String stackTrace = ExceptionUtil.getStackTrace(e);
            if (stackTrace.length() > 200)
            {
                stackTrace = stackTrace.substring(0, 200);
            }
            result.put("Action", "FAIL");
            result.put("Message", stackTrace);
        }finally
        {
            ContextUtil.pushContext(context);
        }

        return result;
    }

    /**
     * 属性组添加属性
     *
     * @param context
     * @param attributeName
     */
    public static String addStringGroupAttribute(Context context, String attributeName, String Label, String groupName, String ENO_CSRF_TOKEN) throws FrameworkException
    {
        return addGroupAttribute(context, attributeName, Label, groupName, ENO_CSRF_TOKEN);
    }


    /**
     * 属性组添加属性
     *
     * @param context
     * @param attributeName
     */
    public static String addGroupAttribute(Context context, String attributeName, String Label, String groupName, String ENO_CSRF_TOKEN) throws FrameworkException
    {

        String classificationAttribute = "";

        //属性名称
        String Name = attributeName;

        //属性组名称
        String objectName = groupName;

        //查询属性mql
        String queryAttr = "list attribute '" + Name + "'";
        //判断属性是否存在
        String queryAttrResult = MqlUtil.mqlCommand(context, queryAttr);


        //如果不存在就创建并添加到属性组
        if (UIUtil.isNullOrEmpty(queryAttrResult))
        {
            //获取创建 库分类属性的参数 map
            Map paramMap = getCreateGroupAttributeMap(attributeName, Label, groupName, ENO_CSRF_TOKEN);

            classificationAttribute = ClassificationAttributesCreationUtil.createClassificationAttribute(context, paramMap);
        } else
        {//如果已存再的就判断是不是已经添加到属性组
            classificationAttribute = Name;
        }

        //查询属性组属性mql
        String queryInterfaceAttrMql = "print interface '" + objectName + "' select attribute[" + Name + "] dump |";
        //查询属性组属性 结果
        String queryInterfaceAttrResult = MqlUtil.mqlCommand(context, queryInterfaceAttrMql);
        if (UIUtil.isNotNullAndNotEmpty(queryInterfaceAttrResult) && queryInterfaceAttrResult.toUpperCase().equals("TRUE"))
        {//属性组已添加属性

        } else
        {//属性组未添加已存再的属性
            String addInterfaceAttrMql = "mod interface '" + objectName + "' add attribute '" + Name + "'";
            //属性组添加属性
//                ContextUtil.pushContext(context);
            MqlUtil.mqlCommand(context, addInterfaceAttrMql);
//                ContextUtil.popContext(context);
        }

        return classificationAttribute;

    }


    /**
     * 获取创建 库分类属性的参数 map
     *
     * @param attributeName
     * @param Label
     * @param groupName
     * @return
     */
    public static Map getCreateGroupAttributeMap(String attributeName, String Label, String groupName, String ENO_CSRF_TOKEN)
    {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        Locale local = new Locale("zh_CN");

        paramMap.put("Name", attributeName);
        paramMap.put("Type", "string");
        paramMap.put("IsMultivalue", "false");
        paramMap.put("Description", "");
        paramMap.put("Default", "");
        paramMap.put("Choices", "");
        paramMap.put("Minimum", "");
        paramMap.put("Maximum", "");
        paramMap.put("Dimensions", "");
        paramMap.put("PreferredUnit", "");
        paramMap.put("BossPredicate", "");
        paramMap.put("Parametric", "");
        paramMap.put("objectName", groupName);
        paramMap.put("Label", Label);
        paramMap.put("forODT", "");
        paramMap.put("localeObject", local);
        paramMap.put("timeZone", "-8");
        paramMap.put("StringResourceFileId", "emxLibraryCentralStringResource");
//        &csrfTokenName=ENO_CSRF_TOKEN&ENO_CSRF_TOKEN=H31P-UYRQ-KPKD-9CVF-DJ4E-KVQZ-91QC-O5H0
        paramMap.put("csrfTokenName", "ENO_CSRF_TOKEN");
        paramMap.put("ENO_CSRF_TOKEN", ENO_CSRF_TOKEN);
        return paramMap;
    }


    /**
     * 获取创建 库分类属性的参数 map
     *
     * @param attributeName 属性name
     * @param Label         属性显示名称
     * @param groupName     属性组名称
     * @param paraMap       自定义属性
     * @return
     */
    public static Map getCreateGroupAttributeMap(String attributeName, String Label, String groupName, String ENO_CSRF_TOKEN, Map paraMap)
    {
        Map paramMap = getCreateGroupAttributeMap(attributeName, Label, ENO_CSRF_TOKEN, groupName);
        paramMap.putAll(paraMap);
        return paramMap;
    }

    /**
     * 获取库分类的属性组
     *
     * @param context 上下文
     * @param classId 库分类的id
     * @return
     */
    public static MapList getClassGroup(Context context, String classId) throws MatrixException
    {
        Map classMap = new HashMap();
        classMap.put("objectId", classId);
        String[] classArgs = JPO.packArgs(classMap);
        //查询所有的属性组
        MapList groupList = JPO.invoke(context, "emxMultipleClassificationClassification", null, "getAllAttributeGroups", classArgs, MapList.class);
        return groupList;
    }


    /**
     * 获取属性组的使用位置
     *
     * @param context
     * @param groupId
     * @return
     * @throws MatrixException
     */
    public static MapList getWhereUsed(Context context, String groupId) throws MatrixException
    {
        Map usedMap = new HashMap();
        usedMap.put("AGName", groupId);
        String[] userdArgs = JPO.packArgs(usedMap);
        MapList usedList = JPO.invoke(context, "emxMultipleClassificationAttributeGroup", null, "getClassificationsWhereUsed", userdArgs, MapList.class);
        return usedList;
    }
}
